package com.goldgov.meetingstatistics.service.impl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.goldgov.branchCircle.dao.BranchCircleDao;
import com.goldgov.conference.service.ConferenceStatQuery;
import com.goldgov.meetingstatistics.bean.MeetingOrg;
import com.goldgov.meetingstatistics.bean.MeetingOrgQuery;
import com.goldgov.meetingstatistics.bean.MeetingParamsBean;
import com.goldgov.meetingstatistics.bean.MeetingStatisticsBean;
import com.goldgov.meetingstatistics.bean.OrgMeetingNumber;
import com.goldgov.meetingstatistics.bean.OrgMeetingSign;
import com.goldgov.meetingstatistics.bean.OrgMeetingTypeNumber;
import com.goldgov.meetingstatistics.dao.MeetingStatisticsDao;
import com.goldgov.meetingstatistics.service.MeetingStatisticsService;
import com.goldgov.synchronous.service.ConferenceSynchronous;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.PropertyUtil;
@Service
public class MeetingStatisticsServiceImpl implements MeetingStatisticsService {

	@Autowired
	private MeetingStatisticsDao meetingStatisticsDao;
	
	@Autowired
	private BranchCircleDao branchCircleDao;

	@Override
	public List<OrgMeetingNumber> getMeetingTime(MeetingParamsBean meetingParamsBean) {
		List<OrgMeetingNumber> list = new ArrayList<>();
		if (PropertyUtil.objectNotEmpty(meetingParamsBean.getSearchType()) ) {
			if (meetingParamsBean.getSearchType().intValue() == 5) {
				list = meetingStatisticsDao.findConOrgMettingYear(meetingParamsBean);
			}else {
				OrgMeetingNumber meetingNumber = meetingStatisticsDao.findConOrgMettingMonthOrQuarter(meetingParamsBean);
				if (meetingParamsBean.getSearchType().intValue() == 3) {
					list.add(new OrgMeetingNumber(1, meetingNumber.getMonth1()));
					list.add(new OrgMeetingNumber(2, meetingNumber.getMonth2()));
					list.add(new OrgMeetingNumber(3, meetingNumber.getMonth3()));
					list.add(new OrgMeetingNumber(4, meetingNumber.getMonth4()));
					list.add(new OrgMeetingNumber(5, meetingNumber.getMonth5()));
					list.add(new OrgMeetingNumber(6, meetingNumber.getMonth6()));
					list.add(new OrgMeetingNumber(7, meetingNumber.getMonth7()));
					list.add(new OrgMeetingNumber(8, meetingNumber.getMonth8()));
					list.add(new OrgMeetingNumber(9, meetingNumber.getMonth9()));
					list.add(new OrgMeetingNumber(10, meetingNumber.getMonth10()));
					list.add(new OrgMeetingNumber(11, meetingNumber.getMonth11()));
					list.add(new OrgMeetingNumber(12, meetingNumber.getMonth12()));
				}else if (meetingParamsBean.getSearchType().intValue() == 4) {
					list.add(new OrgMeetingNumber(1, meetingNumber.getQuarter1()));
					list.add(new OrgMeetingNumber(2, meetingNumber.getQuarter2()));
					list.add(new OrgMeetingNumber(3, meetingNumber.getQuarter3()));
					list.add(new OrgMeetingNumber(4, meetingNumber.getQuarter4()));
				}
			}
		}else {
			OrgMeetingNumber meetingNumber = meetingStatisticsDao.findConOrgMettingMonthOrQuarter(meetingParamsBean);
			list.add(new OrgMeetingNumber(1, meetingNumber.getMonth1()));
			list.add(new OrgMeetingNumber(2, meetingNumber.getMonth2()));
			list.add(new OrgMeetingNumber(3, meetingNumber.getMonth3()));
			list.add(new OrgMeetingNumber(4, meetingNumber.getMonth4()));
			list.add(new OrgMeetingNumber(5, meetingNumber.getMonth5()));
			list.add(new OrgMeetingNumber(6, meetingNumber.getMonth6()));
			list.add(new OrgMeetingNumber(7, meetingNumber.getMonth7()));
			list.add(new OrgMeetingNumber(8, meetingNumber.getMonth8()));
			list.add(new OrgMeetingNumber(9, meetingNumber.getMonth9()));
			list.add(new OrgMeetingNumber(10, meetingNumber.getMonth10()));
			list.add(new OrgMeetingNumber(11, meetingNumber.getMonth11()));
			list.add(new OrgMeetingNumber(12, meetingNumber.getMonth12()));
		}
		return list;
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年10月12日 下午4:22:26 
	 */
	@Override
	public List<OrgMeetingSign> getMeetingSign(MeetingParamsBean meetingParamsBean) {
		List<OrgMeetingSign> list = new ArrayList<>();
		if (PropertyUtil.objectNotEmpty(meetingParamsBean.getSearchType()) ) {
			if (meetingParamsBean.getSearchType().intValue() == 5) {
				list = meetingStatisticsDao.findMeetingSignByYear(meetingParamsBean);
			}else {
				OrgMeetingSign meetingSign = meetingStatisticsDao.findMeetingSignByMonthOrQuarter(meetingParamsBean);
				if (meetingParamsBean.getSearchType().intValue() == 3) {
					list.add(new OrgMeetingSign(1, meetingSign.getCheckMonth1()));
					list.add(new OrgMeetingSign(2, meetingSign.getCheckMonth2()));
					list.add(new OrgMeetingSign(3, meetingSign.getCheckMonth3()));
					list.add(new OrgMeetingSign(4, meetingSign.getCheckMonth4()));
					list.add(new OrgMeetingSign(5, meetingSign.getCheckMonth5()));
					list.add(new OrgMeetingSign(6, meetingSign.getCheckMonth6()));
					list.add(new OrgMeetingSign(7, meetingSign.getCheckMonth7()));
					list.add(new OrgMeetingSign(8, meetingSign.getCheckMonth8()));
					list.add(new OrgMeetingSign(9, meetingSign.getCheckMonth9()));
					list.add(new OrgMeetingSign(10, meetingSign.getCheckMonth10()));
					list.add(new OrgMeetingSign(11, meetingSign.getCheckMonth11()));
					list.add(new OrgMeetingSign(12, meetingSign.getCheckMonth12()));
				}else if (meetingParamsBean.getSearchType().intValue() == 4) {
					list.add(new OrgMeetingSign(1, meetingSign.getCheckQuarter1()));
					list.add(new OrgMeetingSign(2, meetingSign.getCheckQuarter2()));
					list.add(new OrgMeetingSign(3, meetingSign.getCheckQuarter3()));
					list.add(new OrgMeetingSign(4, meetingSign.getCheckQuarter4()));
				}
			}
		}else {
			OrgMeetingSign meetingSign = meetingStatisticsDao.findMeetingSignByMonthOrQuarter(meetingParamsBean);
			list.add(new OrgMeetingSign(1, meetingSign.getCheckMonth1()));
			list.add(new OrgMeetingSign(2, meetingSign.getCheckMonth2()));
			list.add(new OrgMeetingSign(3, meetingSign.getCheckMonth3()));
			list.add(new OrgMeetingSign(4, meetingSign.getCheckMonth4()));
			list.add(new OrgMeetingSign(5, meetingSign.getCheckMonth5()));
			list.add(new OrgMeetingSign(6, meetingSign.getCheckMonth6()));
			list.add(new OrgMeetingSign(7, meetingSign.getCheckMonth7()));
			list.add(new OrgMeetingSign(8, meetingSign.getCheckMonth8()));
			list.add(new OrgMeetingSign(9, meetingSign.getCheckMonth9()));
			list.add(new OrgMeetingSign(10, meetingSign.getCheckMonth10()));
			list.add(new OrgMeetingSign(11, meetingSign.getCheckMonth11()));
			list.add(new OrgMeetingSign(12, meetingSign.getCheckMonth12()));
		}
		return list;
		
	}
	/**
	 * 三会一课调度
	 * @date 2018年9月7日 上午10:15:56 
	 * @author hanxiangyu
	 * @param meetingParamsBean
	 * @return
	 */
	public void findConTypeNumByYearOrMonth(MeetingParamsBean meetingParamsBean) throws Exception{
		meetingParamsBean.setStaticYear(DateUtil.getYear(new Date()));
		int month = DateUtil.getMonth(new Date());
		for (int i = 1; i <= month ; i++) {
			meetingParamsBean.setSearchMonth(i);
			List<OrgMeetingTypeNumber> list = meetingStatisticsDao.findConTypeNumByYearOrMonth(meetingParamsBean);
			
			
			
		}
	}

	@Override
	public MeetingStatisticsBean countOrgFinishRateByYear(MeetingParamsBean obj)throws Exception {
		//设置一个组织在时间段内应完成会议的基数
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		int quarterValidNum=MeetingParamsBean.REACH_NUM_QUARTER*DateUtil.quarterNum(sdf.parse(obj.getSearchBeginDate()), sdf.parse(obj.getSearchEndDate()));
		int monthValidNum=MeetingParamsBean.REACH_NUM_MONTH*DateUtil.monthNum(sdf.parse(obj.getSearchBeginDate()), sdf.parse(obj.getSearchEndDate()));
		obj.setSearchQuarterValidNum(quarterValidNum);
		obj.setSearchMonthValidNum(monthValidNum);
		
		ConferenceSynchronous con=meetingStatisticsDao.countOrgFinishRate(obj);
		DecimalFormat df = new DecimalFormat("0.00%");//格式化小数 
		
		MeetingStatisticsBean bean =new MeetingStatisticsBean();
		double conferenceCount1=0,conferenceCount2=0,conferenceCount3=0,conferenceCount4=0,distancelearningCount=0;
		if(PropertyUtil.objectNotEmpty(con.getConferenceCount1())&&con.getConferenceCount1()>0) conferenceCount1=con.getConferenceCount1();
		if(PropertyUtil.objectNotEmpty(con.getConferenceCount2())&&con.getConferenceCount2()>0) conferenceCount2=con.getConferenceCount2();
		if(PropertyUtil.objectNotEmpty(con.getConferenceCount3())&&con.getConferenceCount3()>0) conferenceCount3=con.getConferenceCount3();
		if(PropertyUtil.objectNotEmpty(con.getConferenceCount4())&&con.getConferenceCount4()>0) conferenceCount4=con.getConferenceCount4();
		if(PropertyUtil.objectNotEmpty(con.getDistanceLearningCount())&&con.getDistanceLearningCount()>0) distancelearningCount=con.getDistanceLearningCount();
		if(PropertyUtil.objectNotEmpty(con.getOrgNum())&&con.getOrgNum()>0) {
			if(ConferenceStatQuery.YCJY.equals(obj.getSearchMeetingType())) {
				bean.setMeetingFinishAvg(df.format((double)distancelearningCount/(monthValidNum*con.getOrgNum())));
			}else {
				bean.setMeetingFinishAvg(df.format(((double)conferenceCount1/(quarterValidNum*con.getOrgNum())
						+(double)conferenceCount2/(monthValidNum*con.getOrgNum())
						+(double)conferenceCount3/(monthValidNum*con.getOrgNum())
						+(double)conferenceCount4/(quarterValidNum*con.getOrgNum())
						)/4
				));
			}
		}else {
			bean.setMeetingFinishAvg("0%");
		}
		return bean;
	}
	/**
	 * 查询支部信息并统计支部的会议数量
	 * @date 2018年10月25日 下午1:07:45 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	public List<MeetingOrg> findOrgOrMeetingNumList(MeetingOrgQuery query){
		if (!PropertyUtil.objectNotEmpty(query.getSearchYear())) { //没传年份默认查询当前年
			query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		List<MeetingOrg> list = meetingStatisticsDao.findOrgOrMeetingNumList(query);
//		for (MeetingOrg meetingOrg : list) {
//			String secretary = branchCircleDao.getDutyByID(meetingOrg.getOrganizationID());
//			if (PropertyUtil.objectNotEmpty(secretary)) {
//				meetingOrg.setSecretary(secretary);
//			}
//		}
		return list;
	}
	/**
	 * 查询支部信息并统计支部的考勤情况
	 * @date 2018年10月25日 下午1:07:45 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	public List<MeetingOrg> findOrgAndMeetingSignList(MeetingOrgQuery query){
		if (!PropertyUtil.objectNotEmpty(query.getSearchYear())) { //没传年份默认查询当前年
			query.setSearchYear(String.valueOf(DateUtil.getYear(new Date())));
		}
		List<MeetingOrg> list = meetingStatisticsDao.findOrgAndMeetingSignList(query);
//		for (MeetingOrg meetingOrg : list) {
//			String secretary = branchCircleDao.getDutyByID(meetingOrg.getOrganizationID());
//			if (PropertyUtil.objectNotEmpty(secretary)) {
//				meetingOrg.setSecretary(secretary);
//			}
//		}
		return list;
	}
	
	
	@Override
	public List<OrgMeetingNumber> getDisMeetingTime(MeetingParamsBean meetingParamsBean) {
		List<OrgMeetingNumber> list = new ArrayList<>();
		if (PropertyUtil.objectNotEmpty(meetingParamsBean.getSearchType()) ) {
			if (meetingParamsBean.getSearchType().intValue() == 5) {
				list = meetingStatisticsDao.findDisOrgMettingYear(meetingParamsBean);
			}else {
				OrgMeetingNumber meetingNumber = meetingStatisticsDao.findDisMeetingTimeByMOrQ(meetingParamsBean);
				if (meetingParamsBean.getSearchType().intValue() == 3) {
					list.add(new OrgMeetingNumber(1, meetingNumber.getMonth1()));
					list.add(new OrgMeetingNumber(2, meetingNumber.getMonth2()));
					list.add(new OrgMeetingNumber(3, meetingNumber.getMonth3()));
					list.add(new OrgMeetingNumber(4, meetingNumber.getMonth4()));
					list.add(new OrgMeetingNumber(5, meetingNumber.getMonth5()));
					list.add(new OrgMeetingNumber(6, meetingNumber.getMonth6()));
					list.add(new OrgMeetingNumber(7, meetingNumber.getMonth7()));
					list.add(new OrgMeetingNumber(8, meetingNumber.getMonth8()));
					list.add(new OrgMeetingNumber(9, meetingNumber.getMonth9()));
					list.add(new OrgMeetingNumber(10, meetingNumber.getMonth10()));
					list.add(new OrgMeetingNumber(11, meetingNumber.getMonth11()));
					list.add(new OrgMeetingNumber(12, meetingNumber.getMonth12()));
				}else if (meetingParamsBean.getSearchType().intValue() == 4) {
					list.add(new OrgMeetingNumber(1, meetingNumber.getQuarter1()));
					list.add(new OrgMeetingNumber(2, meetingNumber.getQuarter2()));
					list.add(new OrgMeetingNumber(3, meetingNumber.getQuarter3()));
					list.add(new OrgMeetingNumber(4, meetingNumber.getQuarter4()));
				}
			}
		}else {
			OrgMeetingNumber meetingNumber = meetingStatisticsDao.findDisMeetingTimeByMOrQ(meetingParamsBean);
			list.add(new OrgMeetingNumber(1, meetingNumber.getMonth1()));
			list.add(new OrgMeetingNumber(2, meetingNumber.getMonth2()));
			list.add(new OrgMeetingNumber(3, meetingNumber.getMonth3()));
			list.add(new OrgMeetingNumber(4, meetingNumber.getMonth4()));
			list.add(new OrgMeetingNumber(5, meetingNumber.getMonth5()));
			list.add(new OrgMeetingNumber(6, meetingNumber.getMonth6()));
			list.add(new OrgMeetingNumber(7, meetingNumber.getMonth7()));
			list.add(new OrgMeetingNumber(8, meetingNumber.getMonth8()));
			list.add(new OrgMeetingNumber(9, meetingNumber.getMonth9()));
			list.add(new OrgMeetingNumber(10, meetingNumber.getMonth10()));
			list.add(new OrgMeetingNumber(11, meetingNumber.getMonth11()));
			list.add(new OrgMeetingNumber(12, meetingNumber.getMonth12()));
		}
		return list;
	}

	/** 
	 * @Description: TODO
	 * @author hanxiangyu
	 * @date 2018年10月12日 下午4:22:26 
	 */
	@Override
	public List<OrgMeetingSign> getDisMeetingSign(MeetingParamsBean meetingParamsBean) {
		List<OrgMeetingSign> list = new ArrayList<>();
		if (PropertyUtil.objectNotEmpty(meetingParamsBean.getSearchType()) ) {
			if (meetingParamsBean.getSearchType().intValue() == 5) {
				list = meetingStatisticsDao.findDisCheckByYear(meetingParamsBean);
			}else {
				OrgMeetingSign meetingSign = meetingStatisticsDao.findDisCheckByMonthOrQuarter(meetingParamsBean);
				if (meetingParamsBean.getSearchType().intValue() == 3) {
					list.add(new OrgMeetingSign(1, meetingSign.getCheckMonth1()));
					list.add(new OrgMeetingSign(2, meetingSign.getCheckMonth2()));
					list.add(new OrgMeetingSign(3, meetingSign.getCheckMonth3()));
					list.add(new OrgMeetingSign(4, meetingSign.getCheckMonth4()));
					list.add(new OrgMeetingSign(5, meetingSign.getCheckMonth5()));
					list.add(new OrgMeetingSign(6, meetingSign.getCheckMonth6()));
					list.add(new OrgMeetingSign(7, meetingSign.getCheckMonth7()));
					list.add(new OrgMeetingSign(8, meetingSign.getCheckMonth8()));
					list.add(new OrgMeetingSign(9, meetingSign.getCheckMonth9()));
					list.add(new OrgMeetingSign(10, meetingSign.getCheckMonth10()));
					list.add(new OrgMeetingSign(11, meetingSign.getCheckMonth11()));
					list.add(new OrgMeetingSign(12, meetingSign.getCheckMonth12()));
				}else if (meetingParamsBean.getSearchType().intValue() == 4) {
					list.add(new OrgMeetingSign(1, meetingSign.getCheckQuarter1()));
					list.add(new OrgMeetingSign(2, meetingSign.getCheckQuarter2()));
					list.add(new OrgMeetingSign(3, meetingSign.getCheckQuarter3()));
					list.add(new OrgMeetingSign(4, meetingSign.getCheckQuarter4()));
				}
			}
		}else {
			OrgMeetingSign meetingSign = meetingStatisticsDao.findDisCheckByMonthOrQuarter(meetingParamsBean);
			list.add(new OrgMeetingSign(1, meetingSign.getCheckMonth1()));
			list.add(new OrgMeetingSign(2, meetingSign.getCheckMonth2()));
			list.add(new OrgMeetingSign(3, meetingSign.getCheckMonth3()));
			list.add(new OrgMeetingSign(4, meetingSign.getCheckMonth4()));
			list.add(new OrgMeetingSign(5, meetingSign.getCheckMonth5()));
			list.add(new OrgMeetingSign(6, meetingSign.getCheckMonth6()));
			list.add(new OrgMeetingSign(7, meetingSign.getCheckMonth7()));
			list.add(new OrgMeetingSign(8, meetingSign.getCheckMonth8()));
			list.add(new OrgMeetingSign(9, meetingSign.getCheckMonth9()));
			list.add(new OrgMeetingSign(10, meetingSign.getCheckMonth10()));
			list.add(new OrgMeetingSign(11, meetingSign.getCheckMonth11()));
			list.add(new OrgMeetingSign(12, meetingSign.getCheckMonth12()));
		}
		return list;
	}
	/**
	 * 远程教育
	 * @date 2018年11月23日 下午4:44:27 
	 * @author hanxiangyu
	 * @param query
	 * @return
	 */
	public List<MeetingOrg> findOrgDisMeetingNumOrSignList(MeetingOrgQuery query){
		List<MeetingOrg> list = new ArrayList<>();
		if (PropertyUtil.objectNotEmpty(query.getSearchMeetingNumOrSignType()) && query.getSearchMeetingNumOrSignType().intValue() == 2) {
			//查询考勤
			list = meetingStatisticsDao.findDisMeetingSignList(query);
		}else {
			list = meetingStatisticsDao.findDisMeetingNumList(query);
			//查询次数
		}
		return list ;
	}
	
	
}
